#include "messagedispatcher.h"
#include <QDebug>
void MessageDispatcher::DoHandler(myuint Type, std::shared_ptr<PDU>Msg)
{
    if(Register.count(Type))return Register[Type](Msg);
    qDebug()<<"没有注册";
    return;
}

CUserService *MessageDispatcher::getCuserServer()
{
    return this->CuserServer;
}

FriendService *MessageDispatcher::getFriendServer()
{
    return FriendServer;
}

CSearchService *MessageDispatcher::getCsearchServer()
{
    return CsearchServer;
}

MessageDispatcher::MessageDispatcher(QObject *parent) : QObject(parent)
{
    CuserServer=new CUserService(this);
    FriendServer=new FriendService(this);
    CsearchServer=new CSearchService(this);
    ToRegister();
}

void MessageDispatcher::RegistWork(myuint RecvType, std::function<void (std::shared_ptr<PDU>)> Work)
{
    Register[RecvType]=Work;
}

void MessageDispatcher::ToRegister()
{
    //用户服务
    RegistWork(MSG_TYPE_USER_REGISER_RESPOND,[&](std::shared_ptr<PDU>Msg){
        CuserServer->RecvRegisterRespond(Msg);
    });
    RegistWork(MSG_TYPE_USER_LOGIN_RESPOND,[&](std::shared_ptr<PDU>Msg){
        CuserServer->RecvLoginRespond(Msg);
    });
    //好友服务
    RegistWork(MSG_TYPE_ADD_FRIEND_REQUEST,[&](std::shared_ptr<PDU>Msg){
        FriendServer->RecvAddFriendRequest(Msg);
    });
    RegistWork(MSG_TYPE_FRIEND_INFORM_PULL_RESPOND,[&](std::shared_ptr<PDU>Msg){
        FriendServer->RecvFriendInformRespond(Msg);
    });
    RegistWork(MSG_TYPE_ADD_FRIEND_RESPOND,[&](std::shared_ptr<PDU>Msg){
        FriendServer->RecvAddFriendRespond(Msg);
    });
    RegistWork(MSG_TYPE_PULL_FRIEND_LIST_RESPOND,[&](std::shared_ptr<PDU>Msg){
        FriendServer->RecvFriendListRespond(Msg);
    });
    //搜索服务
    RegistWork(MSG_TYPE_ALLNET_SCAN_ALL_RESPOND,[&](std::shared_ptr<PDU>Msg){
        CsearchServer->RecvAllNetScanRespond(Msg);
    });
    RegistWork(MSG_TYPE_SEARCH_ONLINE_RESPOND,[&](std::shared_ptr<PDU>Msg){
        CsearchServer->RecvOnlineFindRespond(Msg);
    });

}
